<?php
/**
*  会员课程笔记
*/
namespace app\api\model;
use think\Model;

class MemberCourseNote extends Model
{
    /**
    *  列表
    */
    public function _list($where = '', $order_by="mcn.sort asc", $page_size = PAGE_SIZE_API){
        $obj = $this->order($order_by);
        $data = $this->alias('mcn')->where($where)
            ->field('mcn.*, cm.title')
            ->join("course_menu cm", 'cm.id=mcn.cmid', 'LEFT')
            ->order($order_by)
            ->paginate($page_size)
            ->toArray();
        return $data;
    }
    
    /**
    * 课程笔记
    */
    public function _noteList($data){
        if(empty($data['mid'])){
            $this->error = '会员id缺失';
            return false;
        }
        $mid = (int)$data['mid'];
        $where = ['mcn.mid'=>$mid];
        if(!empty($data['cid'])){
            $where['mcn.cid'] = $data['cid'];
        }
        
        return $this->_list($where);
        
    }
    
    /**
    * 添加笔记
    */
    public function _create($data){
        if(empty($data)){
            $this->error = '请输入内容';
        }
        $ret = $this->save($data);
        if(!$ret){
            $this->error = '协议签署失败';
            return false;
        }
        return true;
    }

    /**
     * 编辑笔记
     */
    public function _update($data,$id){
        if(empty($data)){
            $this->error = '请输入内容';
        }

        $Note_info = self::get($id);
        $Note_info->content = $data['content'];
        $Note_info->created = $data['created'];
        $ret = $Note_info->save($data);
        if(!$ret){
            $this->error = '编辑成功';
            return false;
        }
        return true;
    }

    /**
     * 编辑笔记
     */
    public function _delete($id){
        $user = self::get($id);
        $ret = $user->delete();
        if(!$ret){
            $this->error = '删除成功';
            return false;
        }
        return true;
    }
    
    
    /**
    * 通过id获取信息
    */
    public function _getInfoByID($id){
        return $this->where([['id', '=',$id]])->find();
    }
    
    /**
    * 通过条件获取信息
    */
    public function _getInfoByWhere($where){
        return $this->where($where)->find();
    }


}